/**
 * Name class used to break up the full input name into first last and middle
 * Will be able to return the name in different formats such as first name only
 * last name only, first and last, initials and full name.
 * @author StuartMoncrieff
 *
 */
public class Name {
	private String firstName;
	private String middleName;
	private String lastName;

	/**
	 * Looks for existence of middle name and creates first, middle, last names.
	 * @param fullname a string of the full name
	 */
	public Name(String fullname){
		while ((!fullname.equals("  "))&&(fullname.indexOf(' ') == 0)){
			//looks for the case that the first element is a space
			//as long as the whole field is not spaces it will remove the 
			//spaces in front of the name.
			fullname = fullname.substring(1);
		}
		if (fullname.equals("  ")){
			//In the case that spaces have been entered into the field the program
			//will assign default values to keep the program running
			System.out.println("Error in name class, the name cannot be empty spaces");
			this.firstName = "NA";
			this.middleName = "";
			this.lastName = "NA";
		}
		if (fullname.trim().length()==1){
			System.out.println("Name must be made of at least two Strings");
			this.firstName = fullname.trim();
			this.middleName = "";
			this.lastName = "NA";
		}
		else{
			//If there is text in the field the program continues
			int first_space = fullname.indexOf(' ');
			//Looks for the first space in the full name field
			firstName = fullname.substring(0, first_space);
			int second_space = fullname.lastIndexOf(' ');
			//Looks for the second space
			if (first_space == second_space){
				middleName = "";
			}
			else {
				//Should the first and second spaces be at different locations
				//there exists a middle name
				middleName = fullname.substring(first_space+1, second_space);
			}
			lastName = fullname.substring(second_space + 1); 
		}
	}

	/**
	 * Returns the first name
	 * @return firstName string of first name
	 */
	public String getFirstName(){
		return firstName;
	}

	/**
	 * Returns the last name
	 * @return lastName string of the last name
	 */
	public String getLastName(){
		return lastName;
	}

	/**
	 * Gets a string of the first and last name together
	 * @return flName a string of both first and last name
	 */
	public String getFirstAndLastName(){
		String flName;
		flName = firstName+" "+lastName;
		return flName;
	}
	
	/**
	 * Returns the full name of the Name
	 * @return fullName first middle and last name
	 */
	public String getFullName(){
		String fullName;
		fullName = firstName+" "+middleName+" "+lastName;
		return fullName;
	}

	/**
	 * Returns the lastname, firstname format of the name
	 * @return lfname string of last then first name
	 */
	public String getLastCommaFirst(){
		String lfName;
		lfName = lastName+", "+firstName;
		return lfName;
	}

	/**
	 * Prints first initial then last name
	 * @return ifName string of initial then last name
	 */
	public String getInitPeriodLast() {
		String ifName;
		ifName = firstName.charAt(0)+". "+lastName;
		return ifName;
	}

	/**
	 * Returns the initials of the name
	 * @return initials string of the initials of the string
	 */
	public String getInitials(){
		String initials;
		if (middleName.equals("")){
			//if the middle name doesn't exist only use first and last
			initials = ""+firstName.charAt(0)+lastName.charAt(0);
		} 
		else {
			//if the middle name exists use all 3 values
			initials = ""+firstName.charAt(0)+middleName.charAt(0)+lastName.charAt(0);
		}
		return initials;
	}

	/**
	 * Returns the string of middle name
	 * @return middleName a string of the middle name
	 */
	public String getMiddleName() {
		return middleName;
	}
	
	/**
	 * Changes the value of the first name
	 * @param fName string of first name to change to
	 */
	public void setFirstName(String fName){
		firstName = fName;
	}

	/**
	 * Changes the value of the middle name
	 * @param mName string of middle name to change to
	 */
	public void setMiddleName(String mName){
		middleName = mName;
	}
	
	/**
	 * Changes the value of the last name
	 * @param lName string of the last name to change to
	 */
	public void setLastName(String lName){
		lastName = lName;
	}

	
}
